package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.common.cache.CacheNames;
import cn.signalpha.common.enums.StatusEnum;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.redis.cache.RedisCache;
import cn.signalpha.core.tool.utils.*;
import cn.signalpha.modules.shop.entity.Goods;
import cn.signalpha.modules.shop.entity.GoodsBrand;
import cn.signalpha.modules.shop.entity.GoodsServe;
import cn.signalpha.modules.shop.entity.GoodsTag;
import cn.signalpha.modules.shop.service.*;
import cn.signalpha.modules.shop.vo.GoodsVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.Duration;
import java.util.List;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 */
public class GoodsWrapper extends BaseEntityWrapper<Goods, GoodsVO> {

	private static final RedisCache redisCache;
	private static final GoodsService goodsService;
	private static final CouponService couponService;
	private static final GoodsTagService goodsTagService;
	private static final GoodsSkuService goodsSkuService;
	private static final FavoriteService favoriteService;
	private static final FootprintService footprintService;
    private static final GoodsAttrService goodsAttrService;
    private static final GoodsSpecService goodsSpecService;
	private static final GoodsServeService goodsServeService;
	private static final GoodsBrandService goodsBrandService;
	private static final PublicityGoodsService publicityGoodsService;

	static {
		redisCache = SpringUtil.getBean(RedisCache.class);
		goodsService = SpringUtil.getBean(GoodsService.class);
		couponService = SpringUtil.getBean(CouponService.class);
		goodsTagService = SpringUtil.getBean(GoodsTagService.class);
		goodsSkuService = SpringUtil.getBean(GoodsSkuService.class);
		favoriteService = SpringUtil.getBean(FavoriteService.class);
		footprintService = SpringUtil.getBean(FootprintService.class);
		goodsAttrService = SpringUtil.getBean(GoodsAttrService.class);
		goodsSpecService = SpringUtil.getBean(GoodsSpecService.class);
		goodsServeService = SpringUtil.getBean(GoodsServeService.class);
		goodsBrandService = SpringUtil.getBean(GoodsBrandService.class);
		publicityGoodsService = SpringUtil.getBean(PublicityGoodsService.class);
	}

	public static GoodsWrapper build() {
		return new GoodsWrapper();
	}

	@Override
	public GoodsVO entityVO(Goods goods) {
		GoodsVO goodsVO = Objects.requireNonNull(BeanUtil.copy(goods, GoodsVO.class), "商品已失效");
		goodsVO.setGoodsSkuList(goodsSkuService.listByGoodsId(goods.getId()));
		goodsVO.setGoodsAttrList(goodsAttrService.listByGoodsId(goods.getId()));
		goodsVO.setGoodsSpecKeyList(goodsService.formattingGoodsSpec(goodsSpecService.listByGoodsId(goods.getId())));

		if(ObjectUtil.isNotEmpty(goodsVO.getBrandId()) && goodsVO.getBrandId() > 0){
			GoodsBrand goodsBrand = goodsBrandService.getById(goodsVO.getBrandId());
			if(ObjectUtil.isNotEmpty(goodsBrand) && goodsBrand.getStatus().equals(StatusEnum.ON.getType())){
				goodsVO.setGoodsBrand(goodsBrand);
			}
		}
		if(StringUtil.isNotBlank(goodsVO.getTagId())){
			List<GoodsTag> goodsTagList = goodsTagService.list(Wrappers.<GoodsTag>lambdaQuery()
				.in(GoodsTag::getId, Func.toLongList(goodsVO.getTagId()))
				.eq(GoodsTag::getStatus, StatusEnum.ON.getType())
				.orderByDesc(GoodsTag::getSort)
			);
			goodsVO.setTagList(goodsTagList);
		}
		if(StringUtil.isNotBlank(goodsVO.getServeId())){
			List<GoodsServe> goodsTagList = goodsServeService.list(Wrappers.<GoodsServe>lambdaQuery()
				.in(GoodsServe::getId, Func.toLongList(goodsVO.getServeId()))
				.eq(GoodsServe::getStatus, StatusEnum.ON.getType())
				.orderByDesc(GoodsServe::getSort)
			);
			goodsVO.setServeList(goodsTagList);
		}
		return goodsVO;
	}

	public GoodsVO entityVO(Goods goods, Long userId) {
		GoodsVO goodsVO = entityVO(goods);
		//用户足迹
		footprintService.saveFootprint(userId, goodsVO.getId());
		//是否收藏
		goodsVO.setFavoriteId(favoriteService.getUserFavoriteId(userId, goodsVO.getId()));
		//可用的优惠券
		goodsVO.setCouponList(couponService.listByGoods(goodsVO.getId()));
		//参与的优惠活动
		goodsVO.setPublicityGoodsList(publicityGoodsService.listByGoodsId(goodsVO.getId()));
		//增加点击量
		String key = CacheNames.USER_CLICK + userId + goodsVO.getId();
		if(StringUtil.isBlank(redisCache.get(key))){
			redisCache.setEx(key, StringPool.ONE, Duration.ofHours(24));
			goodsService.updateById(Goods.builder().id(goodsVO.getId()).clickNum(goodsVO.getClickNum() + 1).build());
		}
		return goodsVO;
	}

}
